package com.akk.config.shiro;

import com.akk.config.redis.RedisCacheManager;
import com.akk.config.redis.RedisSessionDAO;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.session.mgt.SessionManager;
import org.apache.shiro.session.mgt.eis.SessionDAO;
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.*;

/**
 * Shiro配置
 *
 */
@SuppressWarnings("SpringJavaAutowiringInspection")
@Configuration
public class ShiroConfig {

    @Bean(name="shiroFilter")
    public ShiroFilterFactoryBean shirFilter(SecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilter = new ShiroFilterFactoryBean();
        shiroFilter.setSecurityManager(securityManager);
        shiroFilter.setLoginUrl("/console/index.html");
        shiroFilter.setSuccessUrl("/console/index.html");
        shiroFilter.setUnauthorizedUrl("/");

        Map<String, String> filterMap = new LinkedHashMap<>();
        /*=============console=================*/
        filterMap.put("/**","anon");
        //filterMap.put("/**", "authc");



        shiroFilter.setFilterChainDefinitionMap(filterMap);
        return shiroFilter;
    }

    @Bean(name = "lifecycleBeanPostProcessor")
    public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
        return new LifecycleBeanPostProcessor();
    }

    @Bean
    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
        DefaultAdvisorAutoProxyCreator proxyCreator = new DefaultAdvisorAutoProxyCreator();
        proxyCreator.setProxyTargetClass(true);
        return proxyCreator;
    }

    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();
        advisor.setSecurityManager(securityManager);
        return advisor;
    }

    @Configuration
    protected static class ShiroSessionConfiguration {

        @Autowired
        private RedisTemplate redisTemplate;

        @Bean
        public SessionDAO sessionDAO() {
            return new RedisSessionDAO(redisTemplate);
        }

        @Bean(name = "sessionManager")
        public DefaultWebSessionManager sessionManager() {

            final DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
            sessionManager.setGlobalSessionTimeout(30*60*1000);//30分钟
            sessionManager.setSessionDAO(sessionDAO());
            sessionManager.setSessionValidationSchedulerEnabled(true);
            sessionManager.setCacheManager(redisCacheManager());
            return sessionManager;
        }
        @Bean(name = "securityManager")
        public SecurityManager securityManager(AdminRealm adminRealm, SessionManager sessionManager) {
            DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
            securityManager.setRealm(adminRealm);
            securityManager.setSessionManager(sessionManager);
            securityManager.setCacheManager(redisCacheManager());
            return securityManager;
        }
        @Bean
        public RedisCacheManager redisCacheManager() {
            return new RedisCacheManager(redisTemplate);
        }
    }

}
